﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Manufacturing;

namespace NotebookS2
{
    public partial class FND_BOM : RibbonForm
    {
        public FND_BOM()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string SelectedBOM { get; set; }
        public string SelectedBOMName { get; set; }

        void BindGrid()
        {
            BOMManager oManager = new BOMManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table ;
            if (cboSearch.SelectedIndex == 1)
            {
                _table = oManager.BrowseStructureByName(txtSearch.Text);
            }
            else
            {
                _table = oManager.BrowseStructureByCode(txtSearch.Text);
            }
            oManager.Close();

            gridControl1.DataSource = _table;
            gridControl1.Refresh();
        }

        private void FND_BOM_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BindGrid();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;

            this.SelectedBOM = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["BOMCode"]).ToString();
            this.SelectedBOMName = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["BOMName"]).ToString();

            this.Close();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                BindGrid();
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }
    }
}
